package com.wkhyapp.lm.view;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidong.photopicker.PhotoPickerActivity;
import com.lidong.photopicker.SelectModel;
import com.lidong.photopicker.intent.PhotoPickerIntent;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.constant.Constant;
import com.wkhyapp.lm.contract.UserPresenter;
import com.wkhyapp.lm.contract.UserView;
import com.wkhyapp.lm.http.net.UpdateUserRequest;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.utils.IdUtils;
import com.wkhyapp.lm.utils.ImageLoadUtil;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

public class UserInfoActivity extends SuperActivity<UserPresenter> implements UserView {
    private static final int REQUEST_CAMERA_CODE = 10;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private ArrayList<String> imgList = new ArrayList<>();
    @BindView(R.id.back_iv)
    ImageView back_iv;
    Member member;
    @BindView(R.id.head_iv)
    ImageView head_iv;
    @BindView(R.id.nick_et)
    EditText nick_et;
    @BindView(R.id.qq_et)
    EditText qq_et;
    @BindView(R.id.weixin_et)
    EditText weixin_et;
    @BindView(R.id.phone_et)
    EditText phone_et;
    @BindView(R.id.zhanghao_tv)
    TextView zhanghao_tv;
    @BindView(R.id.jianjie_et)
    EditText jianjie_et;
    private String headUrl;
    @BindView(R.id.update_tv)
    TextView update_tv;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {

            this.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }

        mPresenter.getData(MemberUtils.getUid());
    }


    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        head_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPickerIntent intent = new PhotoPickerIntent(UserInfoActivity.this);
                intent.setSelectModel(SelectModel.MULTI);
                intent.setShowCarema(false); // 是否显示拍照
                intent.setMaxTotal(1); // 最多选择照片数量，默认为6
                intent.setSelectedPaths(imagePaths); // 已选中的照片地址， 用于回显选中状态
                startActivityForResult(intent, REQUEST_CAMERA_CODE);
            }
        });

        update_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if("".equals(nick_et.getText().toString())){
                    TToast("请输入昵称");
                    return;
                }

                if("".equals(weixin_et.getText().toString())){
                    TToast("请输入微信账号");
                    return;
                }

                if("".equals(phone_et.getText().toString())){
                    TToast("请输入手机号码");
                    return;
                }



                if("".equals(jianjie_et.getText().toString())){
                    TToast("请输入个人简介");
                    return;
                }

                UpdateUserRequest request = new UpdateUserRequest();
                request.setUserId(MemberUtils.getUid());
                request.setNickname(nick_et.getEditableText().toString().trim());
                request.setPhone(phone_et.getEditableText().toString().trim());
                request.setQqAccount(qq_et.getEditableText().toString().trim());
                request.setRemark(jianjie_et.getEditableText().toString().trim());
                request.setWechatAccount(weixin_et.getEditableText().toString().trim());
                showLoad("正在提交...");
                mPresenter.update(request);
            }
        });
    }

    @Override
    public void setUser(Member member) {
        this.member = member;
        ImageLoadUtil.setCirlcleImage_Normals(mContext,member.getHeadImg(),head_iv);
        nick_et.setText(member.getNickname());
        zhanghao_tv.setText("账号:"+member.getId());

        if(StringUtils.isEmpty(member.getQqAccount())){
            qq_et.setHint("请输入QQ号码");
        }else{
            qq_et.setText(member.getQqAccount()+"");
        }

        if(StringUtils.isEmpty(member.getWechatAccount())){
            weixin_et.setHint("请输入微信号");
        }else{
            weixin_et.setText(member.getWechatAccount()+"");
        }

        if(StringUtils.isEmpty(member.getMobile())){
            phone_et.setHint("请输入联系手机");
        }else{
            phone_et.setText(member.getMobile()+"");
        }

        jianjie_et.setText(member.getRemark()+"");

    }

    @Override
    public void updateSucc() {
        dismissLoad();
        TToast("个人信息修改成功");
    }

    Configuration config = new Configuration.Builder().build();
    UploadManager uploadManager = new UploadManager(config);
    @Override
    public void setToken(String token) {
        String key = "head_"+MemberUtils.getMember()+"_"+System.currentTimeMillis()+"_"+ IdUtils.getShartUUID();
        uploadManager.put(imgList.get(0), key, token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        if(info.isOK()) {
                            headUrl = Constant.qiniuUrl+key;
                            ImageLoadUtil.setCirlcleImage_Normals(mContext,headUrl,head_iv);
                            UpdateUserRequest request = new UpdateUserRequest();
                            request.setUserId(MemberUtils.getUid());
                            request.setHeadImg(headUrl);
                            mPresenter.updateHead(request);
                        }
                    }
                }, null);
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_info;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                // 选择照片
                case REQUEST_CAMERA_CODE:
                    ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT);
                    Log.d(TAG, "数量：" + list.size());
                    imgList.addAll(list);
                    if(list.size()>0){
                        showLoad("正在修改...");
                        mPresenter.getToken();
                    }
                    break;
            }
        }
    }


    @Override
    protected UserPresenter createPresenter() {
        return mPresenter = new UserPresenter(this);
    }




}
